package android.wordcap;

import java.util.Random;
import android.app.Activity;
import android.content.ClipData;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

public class Game extends Activity {
	private int count = 0;
	private int half_random_child = 0;
	private int length = 0;
	private int score = 0;
	private int hinted = 0;
	private int wrong = 0;
	private char[] hint;

	public void addBlankDrop() {
		final LinearLayout layout = (LinearLayout) findViewById(R.id.drop);
		final RelativeLayout parent = new RelativeLayout(this);
		parent.setTag(count++);
		parent.setOnDragListener(new View.OnDragListener() {
			@Override
			public boolean onDrag(View v, DragEvent event) {
				if (event.getAction() == DragEvent.ACTION_DROP) {
					String string = event.getClipDescription().getLabel()
							.toString();
					// change image
					parent.setBackgroundResource(Game.this.getResources()
							.getIdentifier("game_" + string + "true",
									"drawable", Game.this.getPackageName()));
					// invisible
					findViewById(R.id.drag).findViewWithTag(count)
							.setVisibility(View.INVISIBLE);
					final ImageView symbol = new ImageView(Game.this);
					parent.addView(symbol);
					symbol.getViewTreeObserver().addOnGlobalLayoutListener(
							new OnGlobalLayoutListener() {

								@SuppressWarnings("deprecation")
								@Override
								public void onGlobalLayout() {
									LayoutParams lp = new LayoutParams(
											LayoutParams.WRAP_CONTENT,
											LayoutParams.WRAP_CONTENT);
									lp.topMargin = parent.getHeight()
											- symbol.getHeight();
									lp.leftMargin = parent.getWidth()
											- symbol.getWidth();
									symbol.setLayoutParams(lp);
									symbol.getViewTreeObserver()
											.removeGlobalOnLayoutListener(this);
								}
							});
					if (string.equals("" + hint[(Integer) v.getTag()])) {
						symbol.setImageResource(R.drawable.game_correct);
						hint[(Integer) v.getTag()] = '\0';
						score += 10;
						((TextView) findViewById(R.id.score)).setText(Integer
								.toString(score));
						if (--length == 0) {
							// goto next activity
							Intent intent = new Intent(Game.this,
									Summary.class);
							int[] a = { score, hinted, wrong };
							intent.putExtra("score", a);
							startActivity(intent);
							finish();
						}
					} else {
						symbol.setImageResource(R.drawable.game_wrong);
						score -= 5;
						wrong++;
//if wrong morethan xx goto summary
						((TextView) findViewById(R.id.score)).setText(Integer
								.toString(score));
						new Handler().postDelayed(new Runnable() {
							@Override
							public void run() {
								parent.setBackgroundResource(R.drawable.game_blank);
								symbol.setVisibility(View.GONE);
								findViewById(R.id.drag).findViewWithTag(count)
										.setVisibility(View.VISIBLE);
							}
						}, 1000);

					}
				}
				return true;
			}
		});
		parent.setBackgroundResource(R.drawable.game_blank);
		layout.addView(parent);

	}

	public void addDrag(final char c) {
		LinearLayout layout = (LinearLayout) findViewById((count++ < half_random_child) ? R.id.drag_1
				: R.id.drag_2);
		ImageView imageView = new ImageView(this);
		imageView.setImageResource(this.getResources().getIdentifier(
				"game_" + c + "cap", "drawable", this.getPackageName()));
		imageView.setOnDragListener(new View.OnDragListener() {
			@Override
			public boolean onDrag(View v, DragEvent event) {
				final int action = event.getAction();
				switch (action) {
				case DragEvent.ACTION_DRAG_ENDED:
					v.setAlpha(1);
					break;
				}
				return true;
			}
		});
		layout.addView(imageView);
		((ImageView) imageView).setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent arg1) {
				ClipData data = ClipData.newPlainText(String.valueOf(c), "");
				View.DragShadowBuilder shadow = new View.DragShadowBuilder(v);
				v.startDrag(data, shadow, null, 0);
				v.setAlpha(0);
				v.setTag(++count);
				return false;
			}
		});

	}

	public void skip(View v) {
		Intent intent = getIntent();
		finish();
		startActivity(intent);
	}

	public void hint(View v) {
		Random r = new Random();
		hinted++;
		score -= 10;
		((TextView) findViewById(R.id.score)).setText(Integer.toString(score));
		while (true) {
			int rand = r.nextInt(hint.length);
			if (hint[rand] != '\0') {
				final View view = findViewById(R.id.drop).findViewWithTag(rand);
				view.setBackgroundResource(Game.this.getResources()
						.getIdentifier("game_" + hint[rand] + "true",
								"drawable", Game.this.getPackageName()));
				view.setAlpha((float) 0.5);
				new Handler().postDelayed(new Runnable() {
					@Override
					public void run() {
						view.setBackgroundResource(R.drawable.game_blank);
						view.setAlpha(1);
					}
				}, 1000);
				break;
			}
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// system
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_game);
		getActionBar().hide();
		// create caption
		String caption = "hello";
		length = caption.length();
		hint = caption.toCharArray();
		for (char c : hint) {
			addBlankDrop();
		}
		count = 0;
		// create char
		final String alphabet = "abcdefghijklmnopqrstuvwxyz";
		int condition_eq = 4;
		Random r = new Random();
		char[] c_caption;
		for (int i = 0; i < condition_eq; i++) {
			caption += alphabet.charAt(r.nextInt(26));
		}
		c_caption = caption.toCharArray();
		half_random_child = (c_caption.length + 1) / 2;
		for (int i = c_caption.length - 1; i > 0; i--) {
			int index = r.nextInt(i + 1);
			char a = c_caption[index];
			c_caption[index] = c_caption[i];
			c_caption[i] = a;
		}
		for (char c : c_caption) {
			addDrag(c);
		}
		count = 0;

		// define owner picture and name location
		((TextView) findViewById(R.id.owner_name)).setText("PaLam");
		((ImageView) findViewById(R.id.owner_picture))
				.setImageResource(R.drawable.avt);
		final View view = findViewById(R.id.owner_picture_n_name);
		view.getViewTreeObserver().addOnGlobalLayoutListener(
				new OnGlobalLayoutListener() {
					@Override
					public void onGlobalLayout() {
						LayoutParams lp = (LayoutParams) view.getLayoutParams();
						lp.topMargin = (int) (findViewById(R.id.display_score)
								.getY() - findViewById(R.id.owner_picture)
								.getWidth() / 2);
						view.setLayoutParams(lp);
						view.getViewTreeObserver()
								.removeGlobalOnLayoutListener(this);
					}
				});

	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		// fullscreen mode
		if (hasFocus) {
			getWindow().getDecorView().setSystemUiVisibility(
					View.SYSTEM_UI_FLAG_LAYOUT_STABLE
							| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
							| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
							| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
							| View.SYSTEM_UI_FLAG_FULLSCREEN
							| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
		}
	}
}